package vn.fpt.fsoft.menugame;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;
import java.text.AttributedString;
import java.util.ArrayList;
import java.util.List;

import javax.smartcardio.ATR;

import vn.fpt.fsoft.coreframework.Sprite;

public class MainMenu extends GameScreen {

	private List<MenuItem> menuItems = new ArrayList<MenuItem>();

	private int menuSelect = 0;
	private BufferedImage background;

	public MainMenu(ScreenManager screenManager) {
		super(screenManager);

		Sprite sprite = new Sprite();
		MenuItem mnNewGame = new MenuItem(
				sprite.getSprite("images/MainMenuPlank01.png"), 227, 83,
				"New Game", Color.BLACK);
		menuItems.add(mnNewGame);
		MenuItem mnOption = new MenuItem(
				sprite.getSprite("images/MainMenuPlank01.png"), 227, 177,
				"Options", Color.BLACK);
		menuItems.add(mnOption);
		MenuItem mnHighScore = new MenuItem(
				sprite.getSprite("images/MainMenuPlank01.png"), 227, 270,
				"High Score", Color.BLACK);
		menuItems.add(mnHighScore);
		MenuItem mnHelp = new MenuItem(
				sprite.getSprite("images/MainMenuPlank01.png"), 227, 363,
				"Help", Color.BLACK);
		menuItems.add(mnHelp);
		MenuItem mnQuit = new MenuItem(
				sprite.getSprite("images/MainMenuPlank01.png"), 227, 456,
				"Quit", Color.BLACK);
		menuItems.add(mnQuit);

		background = sprite.getSprite("images/background.png");
	}

//	public MainMenu(ScreenManager screenManager, Listener listener) {
//		super(screenManager);
//		this.listener = listener;
//
//		Sprite sprite = new Sprite();
//		MenuItem mnNewGame = new MenuItem(
//				sprite.getSprite("images/MainMenuPlank01.png"), 227, 83,
//				"New Game", Color.BLACK);
//		menuItems.add(mnNewGame);
//		MenuItem mnOption = new MenuItem(
//				sprite.getSprite("images/MainMenuPlank01.png"), 227, 177,
//				"Options", Color.BLACK);
//		menuItems.add(mnOption);
//		MenuItem mnHighScore = new MenuItem(
//				sprite.getSprite("images/MainMenuPlank01.png"), 227, 270,
//				"High Score", Color.BLACK);
//		menuItems.add(mnHighScore);
//		MenuItem mnHelp = new MenuItem(
//				sprite.getSprite("images/MainMenuPlank01.png"), 227, 363,
//				"Help", Color.BLACK);
//		menuItems.add(mnHelp);
//		MenuItem mnQuit = new MenuItem(
//				sprite.getSprite("images/MainMenuPlank01.png"), 227, 456,
//				"Quit", Color.BLACK);
//		menuItems.add(mnQuit);
//
//		background = sprite.getSprite("images/background.png");
//	}

	@Override
	public void gameUpdate() {
		super.gameUpdate();
		for (int i = 0; i < menuItems.size(); i++) {
			menuItems.get(i).setSelect(menuSelect == i);
		}
	}

	@Override
	public void gameDraw(Graphics2D g2d) {
		g2d.drawImage(background, 0, 0, 800, 600, 0, 0, background.getWidth(),
				background.getHeight(), null);

		for (int i = 0; i < menuItems.size(); i++) {
			menuItems.get(i).draw(g2d);
		}
	}

	@Override
	public void gameKeyPressed(int keyCode) {
		switch (keyCode) {
		case KeyEvent.VK_UP:
			menuSelect--;
			if (menuSelect < 0) {
				menuSelect = 0;
			}
			break;
		case KeyEvent.VK_DOWN:
			menuSelect++;
			if (menuSelect > menuItems.size() - 1) {
				menuSelect = menuItems.size() - 1;
			}
			break;
		case KeyEvent.VK_ENTER:
			fireMenuItem(menuSelect);
			break;
		}
		System.out.println(menuSelect);

	}

	public void selectMenuItem(int select) {
		for (int i = 0; i < menuItems.size(); i++) {
			menuItems.get(i).setSelect(menuSelect == select);
		}
	}

	public int getSelected() {
		for (int i = 0; i < menuItems.size(); i++) {
			if (menuItems.get(i).isSelect()) {
				return i;
			}
		}
		return -1;
	}

	public void fireMenuItem(int menuItem) {
		if (menuItem > -1 && menuItem < menuItems.size()) {
			menuItemSelected(menuItem);
			menuItems.get(menuItem).setSelect(false);
		}
	}

	public void menuItemSelected(int menuItem) {
		GameScreen gameScreen = null;
		switch (menuItem) {
		// menu New Game
		case 0:
			gameScreen = new NewGameScreen(screenManager);
			screenManager.addScreen(gameScreen);
			screenManager.removeScreen(this);
			break;
		// menu Options
		case 1:
			gameScreen = new OptionScreen(screenManager);
			screenManager.addScreen(gameScreen);
			screenManager.removeScreen(this);
			break;
		// menu High Score
		case 2:
			gameScreen = new HighScoreScreen(screenManager);
			screenManager.addScreen(gameScreen);
			screenManager.removeScreen(this);
			break;
		// menu Help
		case 3:
			gameScreen = new HelpScreen(screenManager);
			screenManager.addScreen(gameScreen);
			screenManager.removeScreen(this);
			break;
		// menu Quit
		case 4:
			System.exit(0);
			// screenManager.setDefaultCloseOperation(screenManager.EXIT_ON_CLOSE);
			break;
		}
	}

}
